﻿using System;
using System.Collections.Generic;
using DataAccessLibrary.DAL;
using entities;

namespace BusinessLogicLibrary
{
    public class SupplyModule
    {
        private const string ErrEmptyVal = "ERROR: Empty value";
        private const string Error = "ERROR: ";
        private readonly SupplyDAO _supplyDAO;

        public SupplyModule()
        {
            _supplyDAO = new SupplyDAO();
        }

        public string AddSupplyDetail(bool available, string supplyName, decimal price, string unit)
        {
            try
            {
                if (supplyName.Trim() != "" && unit != "")
                {
                    var supplyDetail = new SupplyDetail()
                                           {
                                               Available = available,
                                               SupplyName = supplyName,
                                               SupplyPrice = price,
                                               Unit = unit
                                           };
                    return _supplyDAO.InsertSupplyDetail(supplyDetail);
                }
                return ErrEmptyVal;
            }
            catch (Exception ex)
            {
                return Error + ex.Message;
            }
        }

        public List<SupplyDetail> GetSupplyDetail(bool available)
        {
            return available ? _supplyDAO.SelectSupplyDetail(1) : _supplyDAO.SelectSupplyDetail(-1);
        }

        public SupplyDetail GetSupplyDetailByID(int id)
        {
            return _supplyDAO.SelectSupplyDetailByID(id);
        }

        public List<SupplyDetail> GetSupplyDetailByName(string name)
        {
            return _supplyDAO.SearchSupplyDetailByName(name);
        }

        public string ModifySupplyDetail(int id, bool available, string supplyName, decimal price, string unit)
        {
            try
            {
                if (supplyName.Trim() != "" && unit != "")
                {
                    var supplyDetail = new SupplyDetail()
                                           {
                                               SupplyDetailID = id,
                                               Available = available,
                                               SupplyName = supplyName,
                                               SupplyPrice = price,
                                               Unit = unit
                                           };
                    return _supplyDAO.UpdateSupplyDetail(supplyDetail);
                }
                return ErrEmptyVal;
            }
            catch (Exception ex)
            {
                return Error + ex.Message;
            }
        }
    }
}